package com.bgrc.hubu12305.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bgrc.hubu12305.entity.UserEntity;
import com.bgrc.hubu12305.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 包名:com.bgrc.hubu12305.config
 * 用户:86182
 * 时间:20211222:192021/12/10
 */
public class ShiroRealm extends AuthorizingRealm {

    @Autowired
    private UserService userService;
    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo authInfo = new SimpleAuthorizationInfo();
        authInfo.addStringPermission("test");

        //System.out.println("执行授权+++++++++");

        return authInfo;
    }

    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken userToken = (UsernamePasswordToken) token;
        UserEntity user = userService.findUserByName(userToken.getUsername());
        //System.out.println("执行认证+++++++++");

        if (user!=null){
            //匹配从数据库获取的到密码与前端传递的密码时候相同
            return new SimpleAuthenticationInfo(user,user.getPwd(),this.getName());
        }
        return null;
    }
}
